package com.lancoo.klgcourseware.ui.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.fragment.app.FragmentTransaction;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.base.BaseKlgActivity;
import com.lancoo.klgcourseware.ui.fragment.klgSubjectFragment.KnowledgeEmptyFragment;
import com.lancoo.klgcourseware.ui.fragment.klgSubjectFragment.KnowledgeEnglishFragment;
import com.lancoo.klgcourseware.ui.fragment.klgSubjectFragment.SubjectKnowledgeFragment;
import com.lancoo.klgcourseware.utils.KlgUtils;

import java.io.File;

import lancoo.com.net.retrofitrxjava.RxApiManager;
import me.jessyan.autosize.internal.CancelAdapt;

/**
 * Created by pengmi on 2019/8/14 14:48
 * Description:
 */
public class KlgCourseWareMainActivity extends BaseKlgActivity implements CancelAdapt {

    public static final String KEY_KLGCODE = "klgCode";
    public static final String KEY_EXPANDTRAIN = "expandTrain";
    private SubjectKnowledgeFragment fragment;


    @Override
    protected int getContentViewId() {
        return R.layout.klg_activity_courseware_main;
    }

    @Override
    protected String getTitleName() {
        return null;
    }

    @Override
    protected void setViewAndUpdata() {
        tv_title.setTextSize(18);
        enterKlgStudyByKlgCode();
    }

    public static void launch(Context mContext, String klgCode, boolean expandTrain) {
        Intent mIntent = new Intent(mContext, KlgCourseWareMainActivity.class);
        mIntent.putExtra(KEY_KLGCODE, klgCode);
//        mIntent.putExtra(KEY_KLGCODE,"CE0603201A001ACAA");
        mIntent.putExtra(KEY_EXPANDTRAIN, expandTrain);
        mContext.startActivity(mIntent);
    }


    private void enterKlgStudyByKlgCode() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        String klgCode = getIntent().getStringExtra(KEY_KLGCODE);
        if (TextUtils.isEmpty(klgCode)) {
            klgCode = "null";
            fragment = new KnowledgeEmptyFragment();
        } else {
            switch (klgCode.substring(0, 1)) {
                case "C":
                case "c":
                    fragment = new KnowledgeEnglishFragment();
                    break;
                default:
                    fragment = new KnowledgeEmptyFragment();
            }
        }
        Bundle mBundle = new Bundle();
        mBundle.putString(KEY_KLGCODE, getIntent().getStringExtra(KEY_KLGCODE));
        mBundle.putBoolean(KEY_EXPANDTRAIN, getIntent().getBooleanExtra(KEY_EXPANDTRAIN, true));
        fragment.setArguments(mBundle);
        ft.replace(R.id.framelayout, fragment, klgCode);
        ft.commitAllowingStateLoss();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isFinishing()) {
            RxApiManager.getInstance().removeAll();
            clearKlgTempFile();
        }
    }


    public void setKlgName(String name) {
        if (tv_title != null) {
            tv_title.setText(name);
        }
    }

    private void clearKlgTempFile() {
        File mRecordTempDir = new File(KlgUtils.abainRecordDir(this).getAbsolutePath(), "klgtemp");
        if (mRecordTempDir.exists()) {
            File[] tempRecordFiles = mRecordTempDir.listFiles();
            for (int i = 0; i < tempRecordFiles.length; i++) {
                File file = tempRecordFiles[i];
                if (file.exists()) {
                    file.delete();
                }
            }
        }
    }
}
